package au.com.handsunited.web.controller.organisation;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;

import au.com.handsunited.model.Organisation;
import au.com.handsunited.web.command.organisation.OrganisationCommand;
import au.com.handsunited.web.session.SessionSupport;

public class OrganisationRegistrationController extends AbstractOrganisationEditController {

	private static Logger logger = LoggerFactory.getLogger(OrganisationRegistrationController.class);


	/**
	 *
	 */
	public OrganisationRegistrationController() {
		super();
	}


	/**
	 * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.validation.BindException)
	 */
	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {

		OrganisationCommand organisationCommand = (OrganisationCommand) command;
		Organisation organisation = organisationCommand.getOrganisation();

		organisation = organisationService.createOrganisation(organisation, SessionSupport.getUser(request), null);

		Map<String, String> model = new HashMap<String, String>();
		model.put("organisationName", organisation.getName());
		model.put("organisationId", organisation.getId().toString());

		ModelAndView mav = new ModelAndView(getSuccessView(), model);
		return mav;
	}

}
